Skip to content

fix: validate fractions in get_dataset to prevent ZeroDivisionError - #7164

Open
CJstate wants to merge 2 commits into
huggingface:mainfrom
CJstate:fix-6981-zero-sum-fractions
Open

fix: validate fractions in get_dataset to prevent ZeroDivisionError#7164
CJstate wants to merge 2 commits into
huggingface:mainfrom
CJstate:fix-6981-zero-sum-fractions

Conversation

@CJstate

@CJstate CJstate commented Sep 10, 2026

Copy link
Copy Markdown

What does this PR do?

Adds construction-time validation for fractions in get_dataset. Zero or negative values were accepted at construction time but caused a ZeroDivisionError (or silently produced negative selection sizes) during dataset loading, as reported in #6981.

The fix adds a check consistent with the existing validation for partial fractions and streaming:

if any(fraction < 0 for fraction in fractions):
    raise ValueError(f"All \`fraction\` values must be non-negative, got {fractions}")
if sum(fractions) <= 0:
    raise ValueError(f"Sum of \`fraction\` values must be positive, got {fractions} (sum={sum(fractions)})")

Also adds parametrized regression tests covering negative fractions and zero-sum fractions.

Fixes #6981

Before submitting

AI writing disclosure

  • AI-assisted: some parts were suggested or improved by AI, but the PR was written and reviewed by a human.

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag members/contributors who may be interested in your PR.


Note

Low Risk
Input validation and tests only; behavior for valid configs is unchanged and failures surface earlier with explicit errors.

Overview
get_dataset now validates dataset mixture fraction values before loading any data, so invalid configs fail fast with clear **ValueError**s instead of ZeroDivisionError or bad row selection during weighting.

When any dataset uses fraction, the mixture must still set it on all or none; additionally, every value must be non-negative and their sum must be positive. Regression tests cover negative fractions and all-zero sums (fixes #6981).

Separately, get_repetition_penalty_reward rejects ngram_size <= 0 at construction time (mirroring existing max_penalty checks), with parametrized tests for 0 and -1.

Reviewed by Cursor Bugbot for commit 8668b23. Bugbot is set up for automated code reviews on this repo. Configure here.

CJstate and others added 2 commits September 9, 2026 15:46
Zero or negative ngram_size values were accepted at construction time but
caused a ZeroDivisionError (or silently empty n-grams) during reward
computation. Add construction-time validation consistent with the existing
max_penalty check.

Fixes huggingface#7015.
- Add construction-time validation for negative fractions and zero-sum fractions
- Reject negative values with clear ValueError
- Require sum of fractions to be positive
- Fixes huggingface#6981

Co-authored-by: CJstate <CJstate@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug] get_dataset raises ZeroDivisionError for zero-sum fractions

1 participant